/*
 * Copyright (C) 2014 Lucid Wolf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.lucidhex.hex;

import com.lucidhex.LucidBoard;
import com.lucidhex.LucidSide;
import com.lucidhex.LucidUnitManager;
import com.lucidhex.LucidXMLObject;
import com.lucidhex.io.LucidWorldParser;
import com.lucidhex.viewer.LucidIcon;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
/**
 *
 * @author Wolf
 */
public class LucidHex implements LucidXMLObject{

    private ArrayList<LucidUnitManager> units = new ArrayList<>();
    private LucidBoard board;
    private LucidHexType type = LucidHexType.NULL_HEX;
    private LucidIndex tempCoord;
    private LucidSide owner = LucidSide.NONE;
    public LucidHex(){}
    public LucidHex(LucidBoard board, LucidHexType type){
        this.board = board;
        this.type = type;
    }
    public void setBoard(LucidBoard board) {
        this.board = board;
        // <>< test for if type exists and add it or Null it?
    }
    public LucidIndex getPosition() {
        if(board != null){
            return board.getPosition(this);
        }
        return tempCoord;
    }
    
    public Color getBackGroundColor(){
        return type.getBackgroundColor();
    }
    public LucidIcon getHexDetail(){
        return type.getForegroundIcon();
    }
    public String getHexType(){
        return type.getTypeName();
    }

    @Override
    public String toString(){
        LucidIndex coord = getPosition();
        return getHexType()+" @ "+coord.A()+","+coord.B()+","+coord.C();
    }
    @Override
    public void getChildren(Collection<Object> children) {
        children.addAll(units);
    }
    @Override
    public String getTag(){
        return "Hex";
    }
    @Override
    public void addField(String id, String attribute) {
        if(id.equalsIgnoreCase("Coord")){
            String[] values = attribute.split(",");
            int a = Integer.parseInt(values[0].trim());
            int b = Integer.parseInt(values[1].trim());
            int c = Integer.parseInt(values[2].trim());
            tempCoord = new LucidIndex(a, b, c);
            board.addHex(tempCoord, this);
        }else if(id.equalsIgnoreCase("Type")){
            type = board.getHexType(attribute);
        }
    }

    @Override
    public void getFields(Map<String, String> attrib) {
        attrib.put("Coord", getPosition().toString());
        attrib.put("Type", type.getTypeName());
    }
    @Override
    public void addChild(Object aChild) {
        
    }
   @Override
    public boolean writeToXml() {
        return true;
    }

    @Override
    public void parseLink(LucidWorldParser parser) {
        setBoard(parser.getParsingWorld().getBoard());
    }
   

}
